// pages/settleIn/index.js
import { areaList } from '../../miniprogram_npm/@vant/area-data/data';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    type:'分类错误会影响审核结果，请仔细确认',
    latitude:'',
    address:'',
    userName:'',
    mobile:'',
    columns: [],
    typeList:[],
    isPopup:false,
    isAddress:false,
    province:'位置有误将会影响客户到店，请仔细确认',
    city:'',
    area:'',
    typeId:'',
    image:'../../static/settleIn/tx.png',
    idHead:'../../static/settleIn/rx.png',
    idEmblem:'../../static/settleIn/gh.png',
    businessLicense:'../../static/settleIn/yyzz.png',
    storeImage:'../../static/settleIn/zp.png',
    areaList
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.typeList()
  },
  typeList(){
    app.agriknow.getClickList().then(res=>{
      console.log(res)
      let columns = []
      res.data.forEach((item,index)=>{
        columns.push(item.name)
      })
      this.setData({
        typeList:res.data,
        columns:columns
      })
    })
  },
  submit(){
    wx.showLoading({
      title: '提交中',
      mask:true
    })
    if(!this.data.name) return this.tip('请输入店铺名称');
    if(!this.data.typeId) return this.tip('请选择店铺分类');
    if(!this.data.area) return this.tip('请选择店铺地址');
    if(!this.data.address) return this.tip('请输入详细地址');
    if(!this.data.userName) return this.tip('请输入店主姓名');
    if(!this.data.mobile) return this.tip('请输入联系方式');
    if(this.data.image=='../../static/settleIn/tx.png') return this.tip('请上传店铺头像');
    if(this.data.storeImage=='../../static/settleIn/zp.png') return this.tip('请上传店铺照片');
    if(this.data.idHead=='../../static/settleIn/rx.png') return this.tip('请上传人像面');
    if(this.data.idEmblem=='../../static/settleIn/gh.png') return this.tip('请上传国徽面');
    // if(this.data.businessLicense=='../../static/settleIn/yyzz.png') return this.tip('请上传营业执照');
    let data = {
      name:this.data.name,
      typeId:this.data.typeId,
      province:this.data.province,
      city:this.data.city,
      area:this.data.area,
      address:this.data.address,
      userName:this.data.userName,
      mobile:this.data.mobile,
      image:this.data.image,
      idHead:this.data.idHead,
      idEmblem:this.data.idEmblem,
      businessLicense:this.data.businessLicense,
      storeImage:this.data.storeImage,
      situation:wx.getStorageSync('locationInfo').location.lng,
      latitude:wx.getStorageSync('locationInfo').location.lat
    }
    app.agriknow.userApplyStore(data).then(res=>{
      console.log(res)
      wx.hideLoading()
      if(res.code=='000000'){
        wx.showModal({
          title: '提示',
          content: '提交成功',
          showCancel:false,
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.redirectTo({
                url: '/pages/examine/index'
              })
            }
          }
        })
      }
    }).catch(res=>{
      wx.hideLoading()
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000,
        mask: true
      })
    })
  },
  upload(e) {
    let index = e.currentTarget.dataset.index
    let that = this
    wx.chooseMedia({
      count:1,
      success (res) {
        console.log(res.tempFiles[0].tempFilePath)
        const tempFilePaths = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          url: app.agriknow._baseUrl+'ht/user/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths,
          name: 'file',
          formData: {
            'user': 'test'
          },
          success (res){
            console.log(res)
            if(index==1){
              that.setData({
                storeImage:JSON.parse(res.data).data
              })
            }else if(index==2){
              that.setData({
                idHead:JSON.parse(res.data).data
              })
            }else if(index==3){
              that.setData({
                idEmblem:JSON.parse(res.data).data
              })
            }else if(index==4){
              that.setData({
                businessLicense:JSON.parse(res.data).data
              })
            }else if(index==5){
              that.setData({
                image:JSON.parse(res.data).data
              })
            }
            
          }
        })
      }
    })
  },
  bindCancel(e){
    this.setData({
      isAddress:false
    })
  },
  bindConfirm(e){
    let address = e.detail.values[0].name + ',' + e.detail.values[1].name + ',' + e.detail.values[2].name
    this.setData({
      isAddress:false,
      region:address,
      province:e.detail.values[0].name,
      city:e.detail.values[1].name,
      area:e.detail.values[2].name
    })
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    Toast(`当前值：${value}, 当前索引：${index}`);
  },
  onCancel(e){
    console.log(e)
    this.setData({
      isPopup:false
    })
  },
  onConfirm(e){
    console.log('1',e.detail.index)
    this.setData({
      isPopup:false,
      type:e.detail.value,
      typeId:this.data.typeList[e.detail.index].id
    })
  },
  openPopup(){
    this.setData({
      isPopup:true
    })
  },
  openAddress(){
    this.setData({
      isAddress:true
    })
  },
  tip(name){
    wx.showToast({
      title: name,
      icon:'none',
      duration: 2000
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})